package com.smart.home.netty.handler;

import com.smart.home.util.CommandDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;

/**
 * 查询有线输入按钮名称
 *
 * @author zhangpeng
 * @version 1.0
 * @date 2017-03-09
 */
public class WireInputButtonNameCommandHandler extends AbstractChannelInboundHandler {

    public WireInputButtonNameCommandHandler(String command) {
        super.setCommand(command);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        String result = ByteBufUtil.hexDump(msg);
        setButtonName(CommandDecoder.wireButtonNameDecoder(result));
        //关闭信息通道
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //TODO 异常信息处理
        ctx.close();
    }

}
